﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using YArchitech.LIB;

namespace YArchitech.Plumbing
{
	public partial class DlgUsedWaterType : System.Windows.Forms.Form
	{
		public DlgUsedWaterType()
		{
			this.InitializeComponent();
			new YJKProcessTools().ShowInTaskbarChange(this);
			base.Icon = (base.Icon = Resource.YArchitechIcon);
			this.InitListView();
			base.CancelButton = this.buttonCancel;
		}

		private void InitListView()
		{
			string[] array = new string[]
			{
				"用水部位名称",
				"最高用水量",
				"单位",
				"用水时间",
				"变化系数"
			};
			this.UsedWaterTypes.Columns.Add("", 0);
			for (int i = 0; i < array.Length; i++)
			{
				this.UsedWaterTypes.Columns.Add(array[i], array[i].Length * 25, HorizontalAlignment.Center);
			}
			this.UsedWaterTypes.ItemSelectionChanged += this.UsedWaterTypes_ItemSelectionChanged;
			this.UsedWaterTypes.DoubleClick += this.UsedWaterTypes_DoubleClick;
			this.PrivateBuilding.Checked = true;
		}

		private void UsedWaterTypes_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
		{
			if (e.IsSelected && e.Item.Tag != null)
			{
				UsedWaterCalculate usedWaterCalculate = (UsedWaterCalculate)e.Item.Tag;
				this.MaxUsedWaterValue.Text = usedWaterCalculate.maxUsedProposal.ToString();
				this.CoefficientValue.Text = usedWaterCalculate.coefficientProposal.ToString();
				this.UsedTimeValue.Text = usedWaterCalculate.usedTimeProposal.ToString();
				this.UsedCountValue.Text = usedWaterCalculate.usedCount.ToString();
			}
		}

		private void UsedWaterTypes_DoubleClick(object sender, EventArgs e)
		{
			this.Apply();
		}

		private void PrivateHouse_CheckedChanged(object sender, EventArgs e)
		{
			this.FillList(WaterCalculateType.PrivateBuilding);
		}

		private void PublicHouse_CheckedChanged(object sender, EventArgs e)
		{
			this.FillList(WaterCalculateType.PublicBuilding);
		}

		private void OtherHouse_CheckedChanged(object sender, EventArgs e)
		{
			this.FillList(WaterCalculateType.OtherBuilding);
		}

		private void FillList(WaterCalculateType type)
		{
			this.UsedWaterTypes.Items.Clear();
			foreach (UsedWaterCalculate usedWaterCalculate in UsedWaterCalculateTypes.Instance.CalculateTypeArray)
			{
				if (usedWaterCalculate.calculateType == type)
				{
					ListViewItem listViewItem = new ListViewItem();
					listViewItem.Tag = new UsedWaterCalculate(usedWaterCalculate);
					listViewItem.Text = "";
					ListViewItem.ListViewSubItem listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = usedWaterCalculate.typeName;
					listViewItem.SubItems.Add(listViewSubItem);
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = usedWaterCalculate.maxUsedRange;
					listViewItem.SubItems.Add(listViewSubItem);
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = usedWaterCalculate.unit;
					listViewItem.SubItems.Add(listViewSubItem);
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = usedWaterCalculate.usedTime;
					listViewItem.SubItems.Add(listViewSubItem);
					listViewSubItem = new ListViewItem.ListViewSubItem();
					listViewSubItem.Text = usedWaterCalculate.coefficientRange;
					listViewItem.SubItems.Add(listViewSubItem);
					this.UsedWaterTypes.Items.Add(listViewItem);
				}
			}
		}

		private void buttonCancel_Click(object sender, EventArgs e)
		{
			base.Close();
		}

		private void buttonOK_Click(object sender, EventArgs e)
		{
			this.Apply();
		}

		private void Apply()
		{
			if (this.UsedWaterTypes.SelectedItems.Count > 0)
			{
				bool flag = false;
				try
				{
					ListViewItem listViewItem = this.UsedWaterTypes.SelectedItems[0];
					if (listViewItem.Tag != null)
					{
						this.calculate = new UsedWaterCalculate((UsedWaterCalculate)listViewItem.Tag);
						this.calculate.maxUsedProposal = Convert.ToDouble(this.MaxUsedWaterValue.Text);
						this.calculate.coefficientProposal = Convert.ToDouble(this.CoefficientValue.Text);
						this.calculate.usedTimeProposal = Convert.ToDouble(this.UsedTimeValue.Text);
						this.calculate.usedCount = Convert.ToDouble(this.UsedCountValue.Text);
						flag = true;
					}
				}
				catch (FormatException ex)
				{
					ex.ToString();
					MessageBox.Show(this, "请输入一个有效的数字！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
				catch (OverflowException ex2)
				{
					ex2.ToString();
					MessageBox.Show(this, "请输入一个有效的数字！", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
				}
				catch (Exception ex3)
				{
					ex3.ToString();
				}
				if (flag)
				{
					base.Close();
				}
			}
		}

		public UsedWaterCalculate calculate;
	}
}
